package com.hbx.controller;

import com.hbx.constant.ResponseStateConstant;
import com.hbx.dto.LogSearchParamDto;
import com.hbx.dto.SysLogDto;
import com.hbx.exception.RspException;
import com.hbx.service.LogsService;
import com.hbx.utils.Result;
import com.hbx.vo.logs.LogsPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/*
 * @Author:hbx
 * 
 * */
@Api(tags = "日志处理器")
@RestController
@RequestMapping("/logs")
public class LogController {

    /**
     * 允许清理的系统日志范围
     */
    private static final List<Integer> ALLOW_CLEAR_SIZE = Arrays.asList(1, 3, 6);

    @Autowired
    private LogsService logsService;

    @RequestMapping(value = "/searchLogs",method = RequestMethod.POST)
    @ApiOperation("搜索日志")
    public Result<LogsPageVo> getLogsInfo(@RequestBody @ApiParam(value = "搜索日至参数",required = true) @Valid
                                              LogSearchParamDto logDto, BindingResult result){
        if (result.hasErrors()){
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR,result.getFieldError().getDefaultMessage());
        }
        return logsService.getLogsInfo(logDto);
    }

    /**
     * 获取系统的前置参数
     * @return
     */
    @RequestMapping(value = "/getSysInitParam",method = RequestMethod.GET)
    @ApiOperation("获取系统日志参数")
    public Result getSysInitParam(){
        Result r = new Result();
        logsService.getSysInitParam(r);
        return r;
    }


    /**
     * 获取系统日志
     * @param logDto
     * @return
     */
    @RequestMapping(value = "/sysLogs",method = RequestMethod.POST)
    @ApiOperation("系统日志")
    public Result sysLogs(@RequestBody @ApiParam(value = "搜索日至参数",required = true) SysLogDto logDto ){
        Result r = new Result();
        logsService.getSysLogInfo(logDto, r);
        return r;
    }

    /**
     * 下载日志
     * @param logDto
     * @param resp
     */
    @RequestMapping(value = "/download",method = RequestMethod.POST)
    @ApiOperation("管理员下载日志")
    public void downloadFile(@RequestBody SysLogDto logDto, HttpServletResponse resp) {

        if (logDto == null){
            throw new RspException(400,"入参错误");
        }

        if (logDto.getType() == null){
            throw new RspException(400,"入参错误");
        }

        if (logDto.getDate() == null){
            throw new RspException(400,"入参错误");
        }
        logsService.downLoadlog(logDto, resp);
    }

    /**
     * 管理员清理系统日志
     * @param cleanTime
     * @return
     */
    @RequestMapping(value = "/clearSysLog", method = RequestMethod.GET)
    @ApiOperation("管理员清理系统日志")
    public Result clearSysLog(@RequestParam Integer cleanTime){
        Result r = new Result();
        if (!ALLOW_CLEAR_SIZE.contains(cleanTime)) {
            return r.setCode(400).setMsg("清理范围异常");
        }
        logsService.clearSysLog(cleanTime, r);
        return r;
    }

    /**
     * 管理员清理操作日志
     * @param cleanTime
     * @return
     */
    @RequestMapping(value = "/clearOperaLog", method = RequestMethod.GET)
    @ApiOperation("管理员清理操作日志")
    public Result clearOperaLog(@RequestParam Integer cleanTime){
        Result r = new Result();
        if (!ALLOW_CLEAR_SIZE.contains(cleanTime)) {
            return r.setCode(400).setMsg("清理范围异常");
        }
        logsService.clearOperaLog(cleanTime, r);
        return r;
    }
}
